/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * Maintenance.java
 */
package panels;

import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import database.DBInterface;

/**
 * This JPanel class creates the panel used for checking the status of a trail.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class CheckStatusPanel extends JPanel
{
	private static final String[] columnTitles = {"Trail Name", "Park Name", "Washout %", "Meltout %", "Open"};
	
	private CardLayout cards;
	private JPanel cardPanel;
	private JScrollPane scrollPane;
	JTable resultsTable;
	
	/**
	 * This constructor takes in parameters, then starts the building of the panel.
	 * @param passedInCards The card Layout that holds the the panels for the GUI
	 * @param passedInPanel The panel that holds all of the card panels.
	 * @param passedInMaint The maintenance panel is passed in so that it is easier for
	 * 						the two panels to communicate between each other.
	 */
	public CheckStatusPanel(CardLayout passedInCards, JPanel passedInPanel)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInPanel;
		setUp();
	}
	
	/**
	 * This method creates the UI for this panel.
	 */
	private void setUp()
	{
		this.setLayout(null);
		cards.addLayoutComponent(this, MainFrame.CHECK_STATUS_PANEL);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(29, 154, 789, 40);
		this.add(scrollPane);
		
		JLabel statusLabel = new JLabel("Trail Status");
		statusLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));
		statusLabel.setBounds(363, 54, 102, 16);
		this.add(statusLabel);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}
	
	/**
	 * This method creates the data and table for when the user opens this panel
	 * @param status The trail name for checking the status of.
	 * @return A 2d array of Strings for the table.
	 */
	protected String[][] createDataForTable(String status)
	{
		DBInterface db = new DBInterface();
		String query = "SELECT * FROM maint_user WHERE trail_name = '" + status + "';";
		ResultSet rsMaint = db.customQuery(query);
		query = "SELECT * FROM trail_inventory WHERE trail_name = '" + status + "';";
		ResultSet rsInven = db.customQuery(query);
		try 
		{
			if(!rsMaint.isBeforeFirst() || !rsInven.isBeforeFirst())
			{
				JOptionPane.showMessageDialog(null, "Sorry, this trail does not exist in the database", 
						"Database Error!", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				Vector<Vector<String>> containingVector = new Vector<Vector<String>>();
				Vector<String> foundRow = new Vector<String>();
				try 
				{
					rsMaint.next();
					rsInven.next();
					foundRow.add(rsMaint.getString("trail_name"));
					foundRow.add(rsInven.getString("park_name"));
					foundRow.add(rsMaint.getString("wash_out"));	
					foundRow.add(rsMaint.getString("melt_out"));	
					foundRow.add(rsMaint.getString("open"));	
					
					db.closeConnection();
				} 
				catch (SQLException exception) 
				{
					exception.printStackTrace();
				}
				containingVector.add(foundRow);
				
				Vector<String> columns = new Vector<String>();
				for(String temp : columnTitles)
				{
					columns.add(temp);
				}
				
				resultsTable = new JTable(containingVector, columns);
				
				scrollPane.add(resultsTable);
				scrollPane.setViewportView(resultsTable);
				DefaultTableCellRenderer centerAlign = new DefaultTableCellRenderer();
				centerAlign.setHorizontalAlignment(JLabel.CENTER);
				resultsTable.setDefaultRenderer(String.class, centerAlign);
			}
		} catch (HeadlessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
